<?php
mysql_connect("localhost","studysag_ecs","Mwansemo70");
mysql_select_db("studysag_ecs");


function export_excel_csv()
{
    /*$sql = "SELECT prd.*, pr_img.fields as img_fields, pr_opt.title, pr_opt.fields, pr_opt.coloum, pr_spc.fields as spc_fields, pr_spc.coloum as spc_coloum FROM site_products as prd 
	left join  site_product_images as pr_img on pr_img.productid = prd.id
	left join  site_product_options as pr_opt on pr_opt.productid = prd.id
	left join  site_product_specification as pr_spc on pr_spc.productid = prd.id";
    $rec = mysql_query($sql) or die (mysql_error());*/
   
   $header = "id \t Terms \t Definition \t URL \t";
   // $num_fields = mysql_num_fields($rec);
   
    //for($i = 0; $i < $num_fields; $i++ )
    //{
     	//$header .= mysql_field_name($rec,$i)."\t";
   //}
   //echo $header;
   $all = mysql_query("select * from tooltip");
   while($product = mysql_fetch_array($all))
   {
	   
	   
	 $datas .= '"'. $product['id'] .'"' . "\t".'"'. $product['terms'] .'"' . "\t".'"'. $product['definition'] .'"'. "\t".'"'. $product['url'] .'"'. "\t". "\n";
	   //$datan .= trim( $datas ) . "\n";
	   
   }
   $datal = str_replace("\r" , "" , $datas);
   //echo $datal;
   //echo $data;
    /*while($row = mysql_fetch_row($rec))
    {
        $line = '';
        foreach($row as $value)
        {                                           
            if((!isset($value)) || ($value == ""))
            {
                $value = "\t";
            }
            else
            {
                $value = str_replace( '"' , '""' , $value );
                $value = '"' . $value . '"' . "\t";
				//echo $value;
            }
			//echo $value;
            $line .= $value;
        }
        $data .= trim( $line ) . "\n";
    }
	//echo $data;
    $data = str_replace("\r" , "" , $data);
	//echo $data;
   
    if ($data == "")
    {
        $data = "\n No Record Found!n";                       
    }*/
   
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=reports.xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    print "$header\n$datal";
}
echo export_excel_csv();
?>